#!/bin/bash
APP=waterflow

starttime=`date +'%Y-%m-%d %H:%M:%S'`

if [ -n "$2" ] ;then
    string=$2
else
    string=`date +'%Y-%m-%d %H:%M:%S'`
fi 
 
array=(`echo $string | tr ' ' ' '` )  

flag=0 
 
for var in ${array[@]}
do
    if [ ${flag} -eq 0 ]; then
		day=$var
	fi
   time=$var
   flag=1
done 

string=$time 
array=(`echo $string | tr ':' ' '` )

hour=${time:0:2}
min=${time:3:2}

echo $day
echo $hour
echo "old" $min

thisString="01 02 03 04 05 06 07 08 09 10 11 12 13 14" # 源字符串
searchString=${min} # 搜索字符串
case $thisString in
	*"$searchString"*) echo "包含(01~15)" 
	min='00'
	;;
	*) echo "不包含(01~15)" 
	;;
esac

thisString="16 17 18 19 20 21 22 23 24 25 26 27 28 29" # 源字符串
searchString=${min} # 搜索字符串
case $thisString in
	*"$searchString"*) echo "包含(16~30)" 
	min='15'
	;;
	*) echo "不包含(16~30)" 
	;;
esac

thisString="31 32 33 34 35 36 37 38 39 40 41 42 43 44" # 源字符串
searchString=${min} # 搜索字符串
case $thisString in
	*"$searchString"*) echo "包含(31~45)" 
	min='30'
	;;
	*) echo "不包含(31~45)" 
	;;
esac

thisString="46 47 48 49 50 51 52 53 54 55 56 57 58" # 源字符串
searchString=${min} # 搜索字符串
case $thisString in
	*"$searchString"*) echo "包含(46~59)" 
	min='45'
	;;
	*) echo "不包含(46~59)" 
	;;
esac

echo $day
echo $hour
echo "new" $min

# 在处理完时间的基础上，进行时间操作

# insert overwrite table ${APP}.ads_region_waterflow (select * from ${APP}.ads_region_waterflow distribute by floor (rand()*5));
ads_region_waterflow="
set hive.input.format=org.apache.hadoop.hive.ql.io.HiveInputFormat;
insert overwrite table ${APP}.ads_region_waterflow
select
    date_format(concat('$day',' ','$hour',':','$min',':00'), 'yyyy-MM-dd HH:mm:ss'),
    Region_ID,
       Flow,
       region_X_End,
       region_Y_End
from
(
    select
        t5.id Region_ID,
        sum(Lagrange_flow) / sum(weight) Flow,
        t5.region_X region_X_End,
        t5.region_Y region_Y_End
    from
    (
        select
            id,
            t4.X region_X,
            t4.Y region_Y,
            t3.X,
            t3.Y,
            Flow_amount,
            sqrt(pow((t4.X - t3.X),2) + pow((t4.Y - t3.Y),2)) distance,
            1 / sqrt(pow((t4.X - t3.X),2) + pow((t4.Y - t3.Y),2)) weight,
            1 / sqrt(pow((t4.X - t3.X),2) + pow((t4.Y - t3.Y),2)) * Flow_amount Lagrange_flow
        from
        (
            select
                id,
                X,
                Y
            from ${APP}.ods_base_region
        )t4
        full join
        (
            select
                t1.Site_ID,
                t1.Flow_amount,
                t2.X,
                t2.Y
            from
            (
                select
                    Site_ID,
                    sum(Flow) Flow_amount
                from ${APP}.ods_waterflow
                where day='$day' and hour='$hour' and min='$min'
                group by Site_ID
            )t1
            left join
            (
                select
                    Site_ID,
                    X,
                    Y
                from ${APP}.ods_site_info
            )t2
            on t1.Site_ID = t2.Site_ID
        )t3
    )t5
    group by t5.id, t5.region_X, t5.region_Y
)t6"


case $1 in
    ads_region_waterflow )
        hive -e "$ads_region_waterflow" 
    ;;
    all )
        hive -e "$ads_region_waterflow" 
    ;;
esac

endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "本次运行时间: "$((end_seconds-start_seconds))"s"

